package com.xiaozheng.captcha.demo.config;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.util.CaptchaUtils;
import cloud.tianai.captcha.common.util.ObjectUtils;
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
import cloud.tianai.captcha.validator.impl.SimpleImageCaptchaValidator;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class XzImageCaptchaValidator extends SimpleImageCaptchaValidator {
    /**
     * 扩展数据
     */
    public static final String EXPAND_KEY = "expand";
    /**
     * 回传数据
     */
    public static final String DATA_KEY = "data";
    /**
     * 计算滑块要背景图的百分比，基本校验
     *
     * @param pos    移动的位置
     * @param maxPos 最大可移动的位置
     * @return float
     */
    @Override
    public float calcPercentage(Number pos, Number maxPos) {
        return super.calcPercentage(pos, maxPos);
    }

    /**
     * 校验滑块百分比
     *
     * @param newPercentage 用户滑动的百分比
     * @param oriPercentage 正确的滑块百分比
     * @return boolean
     */
    @Override
    public boolean checkPercentage(Float newPercentage, Float oriPercentage) {
        return super.checkPercentage(newPercentage, oriPercentage);
    }

    /**
     * 校验滑块百分比
     *
     * @param newPercentage 用户滑动的百分比
     * @param oriPercentage 正确的滑块百分比
     * @param tolerant      容错值
     * @return boolean
     */
    @Override
    public boolean checkPercentage(Float newPercentage, Float oriPercentage, float tolerant) {
        return super.checkPercentage(newPercentage, oriPercentage, tolerant);
    }

    /**
     * 用于生成验证码校验时需要的回传参数
     *
     * @param imageCaptchaInfo 生成的验证码数据
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> generateImageCaptchaValidData(ImageCaptchaInfo imageCaptchaInfo) {
        Map<String, Object> map = new HashMap<>(8);
        // 回传数据
        Object data = imageCaptchaInfo.getData();
        map.put("data", data);
        // 扩展数据
        Object expand = imageCaptchaInfo.getExpand();
        map.put("expand", expand);
        // 类型
        String type = imageCaptchaInfo.getType();
        map.put(TYPE_KEY, type);
        return map;
    }

    /**
     * 校验用户滑动滑块是否正确
     *
     * @param imageCaptchaTrack      包含了滑动轨迹，展示的图片宽高，滑动时间等参数
     * @param sliderCaptchaValidData generateSliderCaptchaValidData(生成的数据
     * @return boolean
     */
    @Override
    public boolean valid(ImageCaptchaTrack imageCaptchaTrack, Map<String, Object> sliderCaptchaValidData) {
        if (!ObjectUtils.isEmpty(imageCaptchaTrack) && !ObjectUtils.isEmpty(sliderCaptchaValidData)) {
            String type = getStringParam(TYPE_KEY, sliderCaptchaValidData, CaptchaTypeConstant.SLIDER);
            // 判断类型
            if (CaptchaUtils.isClickCaptcha(type) || CaptchaUtils.isSliderCaptcha(type)){
               return super.valid(imageCaptchaTrack, sliderCaptchaValidData);
            }
            // 用户输入的验证码结果
            Object data = imageCaptchaTrack.getData();
            // 缓存中的验证码结果
            Object object = sliderCaptchaValidData.get(DATA_KEY);

            // 校验
            return ObjectUtils.nullSafeEquals(data, object);

        }
        return false;
    }
}
